package com.tarena.lbs.content.service.impl;

import com.tarena.lbs.content.dao.repository.ArticleRepository;
import com.tarena.lbs.content.pojo.param.ArticleActionParam;
import com.tarena.lbs.content.pojo.param.ArticleContentParam;
import com.tarena.lbs.content.pojo.po.ActionSearchPO;
import com.tarena.lbs.content.pojo.po.ArticleSearchPO;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleActionPageResultVO;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import com.tarena.lbs.content.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleRepository articleRepository;
    @Override
    public void insertArticle(ArticleContentParam param) {
        ArticleSearchPO po = new ArticleSearchPO();
        BeanUtils.copyProperties(param,po);
        articleRepository.insert(po);
    }

    @Override
    public List<ArticleVO> select(ArticleQuery articleQuery) {
        log.info("前端-文章分页列表查询"+articleQuery);
        List<ArticleVO> list=articleRepository.select(articleQuery);
        return list;
    }

    @Override
    public ArticleVO articleDetail(String id) {
        log.debug("文章详情：{}", id);
        ArticleSearchPO articleSearchPO = articleRepository.articleDetail(id);
        if (articleSearchPO == null) {
            throw new RuntimeException("文章不存在，ID: " + id); // 或使用自定义业务异常
        }
        ArticleVO articleVO = new ArticleVO();
        BeanUtils.copyProperties(articleSearchPO, articleVO);
        return articleVO;

    }

    @Override
    public List<ArticleActionPageResultVO> articleBehavior(ArticleActionParam param) {
        log.debug("用户行为记录：{}", param);
        ActionSearchPO actionSearchPO = new ActionSearchPO();
        BeanUtils.copyProperties(param, actionSearchPO);
        actionSearchPO.setCreateTime(new Date());
        List<ArticleActionPageResultVO> list = articleRepository.articleBehavior(actionSearchPO);
        return list;
    }

    @Override
    public Set<String> articleLabels(ArticleQuery articleQuery) {
        log.info("查询文章标签集合，入参:{}",articleQuery);

        Set<String> articleLabels = articleRepository.articleLabels(articleQuery);
        return articleLabels;
    }

    @Override
    public void delete(Integer id) {
        log.info("删除文章{}",id);
        articleRepository.delete(id);
    }

    @Override
    public void updateArticle(String articleName) {
        log.info("更新文章{}",articleName);
        ArticleSearchPO po = new ArticleSearchPO();
        BeanUtils.copyProperties(articleName,po);
        articleRepository.update(po);
    }

}
